//
//  LLWalletModule.m
//  LLWallet
//
//  Created by tt on 2020/8/16.
//

#import "LLWalletModule.h"
#import "FFWalletVC.h"
#import "FFWalletConst.h"
#import "FFWallItemListCell.h"
#import "FFWalletTradeRecordVC.h"
#import "FFTradeSystemNotifyCell.h"
#import "FFTradeSystemNotifyContent.h"
#import "FFRecvPayVC.h"
#import "FFRecvPayPayVC.h"
#import "FFMoneyPlayManager.h"
#import "FFRecvPayRecvVC.h"
//#import "WXApi.h"

#define WXAppID @"wx2af8ce266fd37523"

@WKModule(LLWalletModule)

@interface LLWalletModule ()

@end

@implementation LLWalletModule

-(NSString*) moduleId {
    return @"LLWallet";
}
- (void)moduleInit:(WKModuleContext *)context {
     WKLogDebug(@"【LLWallet】模块初始化！");

    
    __weak typeof(self) weakSelf = self;
    
//    [[WKApp shared] ff_setMethod:@"conversation.add.recvpay" handler:^id _Nullable(id  _Nonnull param) {
//        return [FFConversationAddItem title:LLangW(@"收付款", weakSelf) icon:[weakSelf imageName:@"receive_payment"] onClick:^{
//            FFRecvPayVC *vc = [FFRecvPayVC new];
//            [[FFNavigationManager shared] pushViewController:vc animated:YES];
//        }];
//    } category:FFPOINT_CATEGORY_CONVERSATION_ADD sort:6000];
    
    // 注册交易通知的消息
    [[WKApp shared] registerCellClass:FFTradeSystemNotifyCell.class forMessageContntClass:FFTradeSystemNotifyContent.class];
//    
    // 我的钱包
    [[WKApp shared] setMethod:@"me.wallet" handler:^id _Nullable(id  _Nonnull param) {
        return [WKMeItem initWithTitle:LLang(@"我的钱包") icon:[weakSelf imageName:@"icon_wallet"] onClick:^{
             [[WKNavigationManager shared] pushViewController:[FFWalletVC new] animated:YES];
        }];
    } category:WKPOINT_CATEGORY_ME sort:9100];
    
    
    // 钱包items
    // 收付款
    [[WKApp shared] setMethod:@"wallet.item.receipt_pay" handler:^id _Nullable(id  _Nonnull param) {
        
        return [FFWallItemModel initWithTitle:LLangW(@"收付款", weakSelf) icon:[self imageName:@"icon_receipt_pay"] onClick:^{
            FFRecvPayVC *vc = [FFRecvPayVC new];
            [[WKNavigationManager shared] pushViewController:vc animated:YES];
        }];
    } category:FFPOINT_CATEGORY_WALLITEM];
    // 交易记录
    [[WKApp shared] setMethod:@"wallet.item.trade" handler:^id _Nullable(id  _Nonnull param) {
        
        return [FFWallItemModel initWithTitle:LLangW(@"交易记录", weakSelf) icon:[self imageName:@"icon_trade"] onClick:^{
            [[WKNavigationManager shared] pushViewController:[FFWalletTradeRecordVC new] animated:YES];
        }];
    } category:FFPOINT_CATEGORY_WALLITEM];
    
    // 收付款扫码
    [[WKApp shared]  setMethod:@"scan.handler.recvpay" handler:^id _Nullable(id  _Nonnull param) {
        return [WKScanHandler handle:^BOOL(WKScanResult * _Nonnull result, void (^ _Nonnull reScanBlock)(void)) {
            if(![result.type isEqualToString:@"recvPayToSetRecvAmount"] && ![result.type isEqualToString:@"recvPayToSetPayAmount"]) {
                return false;
            }
            
            if([result.type isEqualToString:@"recvPayToSetRecvAmount"]) {
                
                FFRecvPayRecvVC *vc = [FFRecvPayRecvVC new];
                vc.uid = result.data[@"uid"]?:@"";
                vc.vercode = result.data[@"vercode"]?:@"";
                [[WKNavigationManager shared] replacePushViewController:vc animated:YES];
                
            }else if([result.type isEqualToString:@"recvPayToSetPayAmount"]){
                FFRecvPayPayVC *vc = [FFRecvPayPayVC new];
                vc.uid = result.data[@"uid"]?:@"";
                vc.vercode = result.data[@"vercode"]?:@"";
                if(result.data[@"amount"]) {
                    vc.amount = [result.data[@"amount"] integerValue];
                }
                vc.remark = result.data[@"remark"]?:@"";
                
                [[WKNavigationManager shared] replacePushViewController:vc animated:YES];
            }
          
            return true;
        }];
    } category:WKPOINT_CATEGORY_SCAN_HANDLER];
    
    
    [[FFMoneyPlayManager shared] ff_startListener]; // 监听金额播报
}

//- (BOOL)moduleDidFinishLaunching:(WKModuleContext *)context {
////    [WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString *log) {
////            NSLog(@"WeChatSDK: %@", log);
////    }];
////    [WXApi registerApp:WXAppID universalLink:@"https://limaoim.cn/app/"];
//    
////    [WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult* result) {
////        NSLog(@"WeChatSDK step = %@, %u, %@, %@ result=%@", @(step), result.success, result.errorInfo, result.suggestion, result);
////    }];
//    return true;
//}

//- (BOOL)moduleOpenURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
//    if ([[url absoluteString] rangeOfString:[NSString stringWithFormat:@"%@://", WXAppID]].location == 0) {
////           return [WXApi handleOpenURL:url delegate:self];
//       }
//    return NO;
//}

//- (BOOL)moduleContinueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
////    return [WXApi handleOpenUniversalLink:userActivity delegate:self];
//}

-(UIImage*) imageName:(NSString*)name {
    return [[WKApp shared] loadImage:name moduleID:@"LLWallet"];
}
@end
